{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Model Inference Notebook\n",
    "\n",
    "Use the trained model for prediction on new data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    },
    "tags": []
   },
   "source": [
    "# Steps\n",
    "\n",
    "1. Split the data into training and test data set\n",
    "1. Clean the data (transform null values)\n",
    "1. Scale necessary attributes (normalization, standardization)\n",
    "1. Save transformed data for model training\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Class to load model and run prediction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import logging\n",
    "import joblib\n",
    "biomarkers = None\n",
    "pipeline = None\n",
    "model = None\n",
    "\n",
    "def transformAndPredict(raw, pipeline, model, dropNonBioMarkers = True) :\n",
    "    print (\"predict\")\n",
    "    # dropping non-bio markers\n",
    "    if dropNonBioMarkers :\n",
    "        dropped = raw.drop(\n",
    "        [\"Age\", \"Unit1\", \"Unit2\", \"HospAdmTime\", \"ICULOS\", \"Gender\", \"Bilirubin_direct\", \"TroponinI\", \"isSepsis\"],\n",
    "          axis=1)\n",
    "    else :\n",
    "        dropped = raw\n",
    "\n",
    "    # perform inference and predict\n",
    "    transformed = pipeline.transform(dropped)\n",
    "    prediction = model.predict(transformed)\n",
    "    results = pd.DataFrame(prediction)\n",
    "    results.columns = [\"issepsis\"]\n",
    "\n",
    "    return results\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load data and run prediction\n",
    "\n",
    "Load semi-colon separated data from disk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "predict\n",
      "    issepsis\n",
      "0        0.0\n",
      "1        0.0\n",
      "2        0.0\n",
      "3        0.0\n",
      "4        0.0\n",
      "5        0.0\n",
      "6        1.0\n",
      "7        0.0\n",
      "8        1.0\n",
      "9        0.0\n",
      "10       0.0\n",
      "11       0.0\n",
      "12       0.0\n",
      "13       1.0\n",
      "14       0.0\n",
      "15       0.0\n",
      "16       0.0\n",
      "17       1.0\n",
      "18       0.0\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "sys.path.append(\"../lib\")\n",
    "from getConfig import *\n",
    "config = getConfig(\"../\")\n",
    "\n",
    " \n",
    "#Load the data to run prediction on, model and pipeline\n",
    "inference_data = pd.read_csv(config.inference_path, sep=\",\")\n",
    "pipeline = joblib.load(config.traintest_path + \"/pipeline.pkl\")\n",
    "model = joblib.load(config.tuned_path + \"rfc_model.pkl\")\n",
    "\n",
    "        \n",
    "# Run prediction\n",
    "results = transformAndPredict(inference_data, pipeline, model, dropNonBioMarkers = True )\n",
    "\n",
    "# print results\n",
    "for index, row in results.iterrows():\n",
    "    issepsis = row[\"issepsis\"]\n",
    "print(results)\n",
    "\n",
    "fp = open(config.experiments_path + \"prediction_results.txt\", \"w\")\n",
    "fp.write(str(results))\n",
    "fp.close()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
